package com.huobao.yunying.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Call;
import okhttp3.Response;

public class SendMsgUtils {

	public static void sengMsg(String phoneNum, final OnSendMsgListener listener){
		final int random = (int) (Math.random() * 999999);
		String msg = "您的验证码为"+random+",30分钟内有效，请勿转告他人！";
		String url = null;
		try {
			url = "http://www.139000.com/send/gsend.asp?name=zhytx&pwd=zhytx1288&dst="+phoneNum+"&msg="+URLEncoder.encode(msg, "GB2312");
		} catch (UnsupportedEncodingException e2) {
		}
		PostFormBuilder builder = OkHttpUtils.post().url(url);
		builder.build().execute(new Callback<String>() {

			@Override
			public String parseNetworkResponse(Response response) throws Exception {
				String result = response.body().string();
				return result;
			}

			@Override
			public void onError(Call request, Exception e) {
				listener.onFail("短信发送失败！");
			}

			@Override
			public void onResponse(String response) {
				String [] strs = response.split("&");
				String str = strs[4];
				strs = str.split("=");
				str = strs[1];
				if(str.equals("0")){
					listener.onSendSuccess(random);
				}else{
					listener.onFail("短信发送失败！");
				}
			}
		});
	}
	
	public interface OnSendMsgListener{
		void onSendSuccess(int yanzhengma);
		void onFail(String error);
	}
}
